package com.scsj.supermarket.view.activity.adressmodel;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.orhanobut.logger.Logger;
import com.scsj.supermarket.R;
import com.scsj.supermarket.adapter.AdressAdapter;
import com.scsj.supermarket.base.DeliveryAddressBase;
import com.scsj.supermarket.bean.AdressBean;
import com.scsj.supermarket.bean.BaseBean;
import com.scsj.supermarket.bean.DeliveryAddressBean;
import com.scsj.supermarket.bean.parameter.UpdateAddressIsDefult;
import com.scsj.supermarket.presenter.DeliveryAddressPresenter;
import com.scsj.supermarket.utils.DividerItemDecoration;
import com.scsj.supermarket.utils.SkipUtils;
import com.scsj.supermarket.utils.SwipeRecyclerView;
import com.scsj.supermarket.view.activity.baseactivitymodel.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import es.dmoral.toasty.Toasty;
import okhttp3.RequestBody;

/**
 * 收货地址列表
 */
public class AdressActivity extends BaseActivity implements View.OnClickListener, DeliveryAddressBase.IView {

    private static final String TAG = "AdressActivity";
    private SwipeRecyclerView mRecycleView;
    private LinearLayout returnImg;
    private List<AdressBean> data;
    private AdressAdapter adressAdapter;
    private LinearLayout add_deliver_adress;
    private DeliveryAddressPresenter deliveryAddressPresenter;
    private List<DeliveryAddressBean.DataBean> mAddressdata;

    @Override
    protected void createView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_adress);
        deliveryAddressPresenter = new DeliveryAddressPresenter(this);
        JSONObject jsonObject = new JSONObject();
        RequestBody requestBody = RequestBody.create(okhttp3.MediaType
                .parse("application/json;charset=utf-8"), jsonObject.toString());
        deliveryAddressPresenter.deliverDAlistData(requestBody);

    }

    @Override
    protected void initView() {
        returnImg = findViewById(R.id.returnImg);
        mRecycleView = findViewById(R.id.adress_rv);
        add_deliver_adress = findViewById(R.id.add_deliver_adress);

    }

    @Override
    protected void initListener() {
        /**
         * 删除地址
         */
        mRecycleView.setRightClickListener(new SwipeRecyclerView.OnRightClickListener() {

            @Override
            public boolean onRightClick(int position, String id) {
                Log.e(TAG, " onRightClick position = " + position);
                mAddressdata.remove(position);

                UpdateAddressIsDefult updateAddressIsDefult = new UpdateAddressIsDefult(
                        new UpdateAddressIsDefult.UserDeliveryAddressBean(
                                mAddressdata.get(position).getUserDeliveryAddress().getId()));
                String s = new Gson().toJson(updateAddressIsDefult);

                Logger.d("删除地址传参" + s);
                RequestBody requestBody = RequestBody.create(okhttp3.MediaType
                        .parse("application/json;charset=utf-8"), s);
                deliveryAddressPresenter.deliverDeleteDAData(requestBody);
                return true;
            }
        });
        /**
         * 默认地址
         */
        mRecycleView.setDefaultClickListener(new SwipeRecyclerView.OnDefaultClickListener() {
            @Override
            public boolean onDefaultClick(int position, String id) {
                Toast.makeText(AdressActivity.this, "默认 position = " + position + "id==" + id, Toast.LENGTH_SHORT).show();

                UpdateAddressIsDefult updateAddressIsDefult = new UpdateAddressIsDefult(
                        new UpdateAddressIsDefult.UserDeliveryAddressBean(
                                mAddressdata.get(position).getUserDeliveryAddress().getId()));
                String s = new Gson().toJson(updateAddressIsDefult);
                Logger.d("默认地址传参" + s);
                RequestBody requestBody = RequestBody.create(okhttp3.MediaType
                        .parse("application/json;charset=utf-8"), s);
                deliveryAddressPresenter.deliverSetDefaultDAData(requestBody);
                return true;
            }
        });
        returnImg.setOnClickListener(this);
        add_deliver_adress.setOnClickListener(this);
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.add_deliver_adress:
//            startActivity(new Intent(getHoldingActivity(), ScanCodeActivity.class));
                SkipUtils.toAdressEdit(this);
                break;
            case R.id.returnImg:
                finish();
                break;
        }
    }

    @Override
    public void showDASuccessMsg(String s, DeliveryAddressBean deliveryAddressBean) {
        this.mAddressdata = deliveryAddressBean.getData();
        adressAdapter = new AdressAdapter(this, deliveryAddressBean.getData());
        //设置布局管理器
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        mRecycleView.setLayoutManager(layoutManager);
        layoutManager.setOrientation(OrientationHelper.VERTICAL);
        mRecycleView.setAdapter(adressAdapter);
        DividerItemDecoration dividerLine = new DividerItemDecoration(DividerItemDecoration.VERTICAL);
        dividerLine.setSize(1);
        dividerLine.setColor(0xffdddddd);
        mRecycleView.addItemDecoration(dividerLine);
        mRecycleView.setHasFixedSize(true);
        mRecycleView.setNestedScrollingEnabled(true);
    }

    @Override
    public void deleteDASuccess(String s, BaseBean deleteDABean) {
        Toasty.success(this, "删除成功！").show();
    }

    @Override
    public void setDefaultDASuccess(String s, BaseBean deleteDABean) {
        Toasty.success(this, "设置默认地址成功！").show();
    }

    @Override
    public void showDAErrorMsg(String s) {
        Toasty.error(this, s).show();
    }
}
